package com.moyq5.mvc.framework.general;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;

/**
 * 分页信息
 *
 * @author mo_yq5
 * @since 2016年4月10日
 */
public class Paging extends PageData {
	@JsonIgnore
	public static final int MAX_PAGE_SIZE = 100;
	/**
	 * 是否查询总记录数
	 */
	@JsonIgnore
	private boolean isCount = true;

	/**
	 * 显示链接数
	 */
	@JsonSerialize
	private int linkCount = 7;

	/**
	 * 总页数
	 */
	@JsonSerialize
	private int totalPage = 1;

	/**
	 * 总记录数
	 */
	@JsonSerialize
	private long totalResult = 0;

	@JsonIgnore
	public long getFirstResult() {
		return (getNowPage() - 1) * getPageSize();
	}

	public boolean getIsCount() {
		return isCount;
	}

	public int getLinkCount() {
		return linkCount;
	}

	public int getNowPage() {
		if (super.getNowPage() < 1) {
			super.setNowPage(1);
		} else if (super.getNowPage() > getTotalPage()) {
			super.setNowPage(getTotalPage() == 0 ? 1 : getTotalPage());
		}
		return super.getNowPage();
	}

	public int getPageSize() {
		if (super.getPageSize() <= 0) {
			super.setPageSize(1);
		} else if (super.getPageSize() > MAX_PAGE_SIZE) {
			super.setPageSize(MAX_PAGE_SIZE);
		}
		return super.getPageSize();
	}


	public int getTotalPage() {
		totalPage = (int) ((getTotalResult() / getPageSize() + (getTotalResult()
				% getPageSize() > 0 ? 1 : 0)));
		return totalPage;
	}

	public long getTotalResult() {
		if (totalResult < 0) {
			totalResult = 0;
		}
		return totalResult;
	}

	public void setIsCount(boolean isCount) {
		this.isCount = isCount;
	}

	public void setLinkCount(int linkCount) {
		this.linkCount = linkCount;
	}

	public void setTotalResult(long totalResult) {
		this.totalResult = totalResult;
	}

}
